package com.neouton.crm.receiptRecords.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neouton.common.util.AuthUtils;
import com.neouton.common.util.PageUtil;
import com.neouton.config.ResponseAPI;
import com.neouton.crm.entity.SysUserEntity;
import com.neouton.crm.payRecords.entity.PaymentRecords;
import com.neouton.crm.payRecords.entity.PaymentRequest;
import com.neouton.crm.payRecords.service.PaymentRecordsService;
import com.neouton.crm.pm.entity.SalesProjectManageVO;
import com.neouton.crm.pm.service.ProjectMemberService;
import com.neouton.crm.receiptRecords.entity.ReceiptRecords;
import com.neouton.crm.receiptRecords.entity.ReceiptTable;
import com.neouton.crm.receiptRecords.service.ReceiptRecordsService;
import com.neouton.crm.sales.entity.SalesFileRelationVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 收票记录
 *
 * @author
 * @date 2023-02-03 15:27:00
 * @Version 1.0
 */
@RestController
@Slf4j
@RequestMapping("/receiptRecord")
public class ReceiptRecordController {

    @Resource
    private ReceiptRecordsService receiptRecordsService;
    @Resource
    private ProjectMemberService projectMemberService;

    /**
     * 修改或保存收票记录
     * @param token
     * @param receiptRecords
     * @return
     */
    @PostMapping(value = "/saveReceiptRecord")
    @ResponseBody
    public ResponseAPI saveReceiptRecord(@RequestHeader("token") String token,@RequestBody ReceiptRecords receiptRecords) {
        return ResponseAPI.successData(receiptRecordsService.saveReceiptRecords(receiptRecords,token));
    }
    /**
     * 查询收票记录
     * @param receiptRecords
     * @return
     */
    @PostMapping("/queryReceiptRecordedMoney/")
    public ResponseAPI queryReceiptRecordedMoney(@RequestBody ReceiptRecords receiptRecords ) {
        List<ReceiptRecords> list =  receiptRecordsService.queryReceiptRecordedMoney(receiptRecords);
        return ResponseAPI.successData(list);
    }


    /**
     * 审批通过
     * @param entity
     * @param token
     * @return
     */
    @PostMapping("/approvedReceiptRecord")
    public ResponseAPI approvedReceiptRecord(@RequestBody ReceiptRecords entity, @RequestHeader("token") String token) {
        return ResponseAPI.successData(receiptRecordsService.approvedReceiptRecord(entity,token,false));
    }
    /**
     * 审批驳回
     * @param entity
     * @param token
     * @return
     */
    @PostMapping("/rejectionReceiptRecord")
    public ResponseAPI rejectionReceiptRecord(@RequestBody ReceiptRecords entity,@RequestHeader("token") String token) {
        return ResponseAPI.successData(receiptRecordsService.rejectionReceiptRecord(entity,token));
    }

    /**
     * 根据审批id获取历史数据
     * @param approvalId
     * @return
     */
    @PostMapping("/queryReceiptRecordedHistory/{approvalId}")
    public ResponseAPI queryReceiptRecordedHistory(@PathVariable(value = "approvalId") Integer approvalId) {
        return ResponseAPI.successData(receiptRecordsService.queryReceiptRecordedHistory(approvalId));
    }

    /**
     * 查询收票记录
     *
     * @param receiptRecords
     * @return
     */
    @PostMapping("/queryReceiptRecordsListInfo/{pageNum}/{pageSize}")
    public ResponseAPI queryReceiptRecordsListInfo(@PathVariable(value = "pageNum") Integer pageNum,
                                                   @PathVariable(value = "pageSize") Integer pageSize,
                                                   @RequestBody ReceiptRecords receiptRecords,
                                                   @RequestHeader("token") String token) {
        List<SysUserEntity> userList = projectMemberService.queryUserList(AuthUtils.getUserId(),"receiptRecords", token);
        IPage<ReceiptRecords> page = new Page<>(pageNum, pageSize, true);
        PageUtil<ReceiptRecords> list = receiptRecordsService.queryReceiptRecordsListInfo(page, receiptRecords, userList, token);
        return ResponseAPI.successData(list);
    }

    /**
     * 查询收票记录
     *
     * @param receiptRecords
     * @return
     */
    @PostMapping("/queryToDoReceiptRecordsListInfo/{pageNum}/{pageSize}")
    public ResponseAPI queryToDoReceiptRecordsListInfo(@PathVariable(value = "pageNum") Integer pageNum,
                                                   @PathVariable(value = "pageSize") Integer pageSize,
                                                   @RequestBody ReceiptRecords receiptRecords,
                                                   @RequestHeader("token") String token) {

        IPage<ReceiptRecords> page = new Page<>(pageNum, pageSize, true);
        PageUtil<ReceiptRecords> list = receiptRecordsService.queryToDoReceiptRecordsListInfo(page, receiptRecords, token);
        return ResponseAPI.successData(list);
    }



    /**
     * 导出Excel
     * @param response
     * @param entity
     * @throws Exception
     */
    @PostMapping("/exportInfo")
    public void exportInfo(HttpServletResponse response, @RequestBody ReceiptRecords entity, @RequestHeader("token") String token) throws Exception{
        List<SysUserEntity> userList = projectMemberService.queryUserList(AuthUtils.getUserId(),"receiptRecords", token);
        List<ReceiptRecords> list = receiptRecordsService.queryReceiptRecordsListInfo(entity, userList, token);
        receiptRecordsService.exportReceiptRecordsInfo(response,list,token);
    }


    @PostMapping("/queryReceiptTablesByIdAndPlanPeriod")
    public ResponseAPI  queryReceiptTablesByIdAndPlanPeriod(@RequestBody ReceiptTable entity) {
        List<ReceiptTable> list = receiptRecordsService.queryReceiptTablesByIdAndPlanPeriod(entity);
        return ResponseAPI.successData(list);
    }

    @PostMapping("/updateReceiptTableData/{approvalId}")
    public ResponseAPI updateReceiptTableData(@PathVariable(value = "approvalId") Integer approvalId) {
        receiptRecordsService.updateReceiptTableData(approvalId);
        return ResponseAPI.success();
    }



    @PostMapping("/getReceiptTableDataByReceiptId/{receiptRecordsId}")
    public ResponseAPI getReceiptTableDataByReceiptId(@PathVariable(value = "receiptRecordsId") Integer receiptRecordsId) {
        List<ReceiptTable> list = receiptRecordsService.getReceiptTableDataByReceiptId(receiptRecordsId);
        return ResponseAPI.successData(list);
    }

    @PostMapping("/deleteReceiptRecordById/{receiptRecordsId}")
    public ResponseAPI deleteReceiptRecordById(@PathVariable(value = "receiptRecordsId") Integer receiptRecordsId) {
       Boolean flag= receiptRecordsService.deleteReceiptRecordById(receiptRecordsId);
       return ResponseAPI.successData(flag);
    }

    /**
     * 修改项目
     *
     * @param receiptRecords
     * @return
     */
    @PostMapping("/updateReceiptRecordAfterPass/")
    public ResponseAPI updateReceiptRecordAfterPass(@RequestBody ReceiptRecords receiptRecords, @RequestHeader("token") String token) {
        receiptRecordsService.updateReceiptRecordAfterPass(receiptRecords,token);
        return  ResponseAPI.successData("success");
    }


    @PostMapping("/queryReceiptRecordFromCache/{receiptRecordsId}")
    public ResponseAPI queryReceiptRecordFromCache(@PathVariable(value = "receiptRecordsId") int receiptRecordsId, @RequestHeader("token") String token) {
        ReceiptRecords receiptRecords = receiptRecordsService.queryReceiptRecordFromCache(receiptRecordsId,token);
        return ResponseAPI.successData(receiptRecords);
    }



}
